<template>
	<view class="body">
		<view class="user-set-userinfo-list u-f-ac u-f-jsp">
			<view>用户名</view>
			<view class="u-f-ac">
				<input type="text" v-model="user.username" />
				<view style="margin-left: 20upx;">
					<!-- <image src="../../static/icons/edit.png" style="width: 40upx; height: 40upx;" 
					mode="widthFix" lazy-load="true"></image> -->
				</view>
			</view>
		</view>	
		<button class="btn" style="margin-top: 50upx;" @click="submit" type="primary">确认修改</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user:{}
				/*{
					id:'5',			//用户id
					nickname:"昵称",				//用户名
					des:"这里应该有一条简介",	//用户简介
					sex:1,						//0为女生，1为男生
					name:'张三',					//真实姓名
					phone:'19141203389',		//电话
					email:'anshuo372@qq.com'	//邮箱
				}*/,
			}
		},
		onLoad(res){
			var that = this;
			this.id = res.id;
			this.$uni.request({
				url:'/user/getUserById'
			}).then((r)=>{
				that.user = r.user
			})
		},
		methods: {
			//验证层
			check(){
				if(!this.user.username || this.user.username == ''){
					uni.showToast({
						title:'用户名不能为空',
						icon:'none'
					});
					return false;
				}
				if(this.user.username.length<2 || this.user.username.length > 12){
					uni.showToast({
						title:'请输入2-12个字符的用户名',
						icon:'none'
					});
					return false;
				}
				if(!(/^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$/).test(this.user.username)){
					uni.showToast({
						title:'用户名只能含有汉字、字母、数字和下划线，且不能以下划线开头',
						icon:'none'
					});
					return false;
				}
				// 	return false;
				// }
			
				return true;
			},
			submit(){
				if(!this.check()){
					return;
				}
				uni.showToast({
					title:'验证通过',
					mask:false,
					duration:1000
				});	
				this.$uni.request({
					url: '/user/updateUserInfo',
					method: 'post',
					data: this.user
				}).then((res) => {
					if (res.code == 200){
						this.$toast.success('修改成功')
						console.log(res)
						uni.reLaunch({
							url: '../user/user_logined'
						})
					} else{
						this.$toast.error(res.msg)
					}
						
				})
				//跳转回个人主页
				//uni.switchTab({
				//	url:'/pages/user/user_logined'
				//});
			},
		}
	}
</script>

<style>
	@import url("/static/css/uni.css");
	@import url("/static/css/form.css");
	.user-set-userinfo-list{
		padding: 20upx;
		border-bottom: 1upx solid #F4F4F4;
	}
	.user-set-userinfo-list>view:first-child{
		font-size: 32upx;
		font-weight: bold;
		color: #9b9b9b;
	}
	.user-set-userinfo-list>view:last-child>input{
		text-align: right;
	}
	.user-set-userinfo-list>view:last-child>view:last-of-type{
		margin-left: 20upx;
		color: #9B9B9B;
	}
</style>
